//
//  HELTypeChooseVC.m
//  HandheldELearn
//
//  Created by wsj2015 on 16/7/12.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELTypeChooseVC.h"
#import "HELNetworkManager.h"
#import "UIScrollView+WSJRefresh_MJ.h"
#import "ContactModel.h"
#import "ContactDataHelper.h"

@interface HELTypeChooseVC ()
{
    NSArray *_rowArr;//row arr
    NSArray *_sectionArr;//section arr
}
@end

@implementation HELTypeChooseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self titleView:@"科室选择"];
//    [self isShowBackBtn:YES];
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight -64);
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
//    [self requestData];
    
    UILabel *label =[UILabel new];
    label.frame =CGRectMake(0, 0, kScreenWidth, 44);
    label.text =@"全部";
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAll)];
    [label addGestureRecognizer:tap];
    label.textAlignment = NSTextAlignmentLeft;
    self.tableView.tableHeaderView = label;
    
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf requestData];
    }];
    [self requestData];
    [self wsj_showLodingGif];
    
}
- (void)selectAll{
    if (self.callBack) {
        self.callBack(nil,nil);
    }
    [self.navigationController popViewControllerAnimated:YES];
    

}
- (void)requestData{
    weakSelf_define;
    
    NSString * hospitalId  =[HELAppManager userInfo].hospitalId;
    if (hospitalId ==nil) {
        return;
    }

     
    [[HELNetworkManager sharedMg] GET:kGetdepartment_list_url parameters:@{@"hospitalId":hospitalId} success:^(NSArray *responseObject) {
        [weakSelf wsj_hideLodingGif];
        if ([responseObject isKindOfClass:[NSArray class]]) {
            
            [weakSelf handleWithData:responseObject];
            [weakSelf.tableView reloadData];
        }
       
//        [weakSelf stopAnimationTitle];
        [weakSelf.tableView.header endRefreshing];
    } failure:^(NSString *err) {
         [weakSelf wsj_hideLodingGif];
//        [weakSelf stopAnimationTitle];
         [weakSelf.tableView.header endRefreshing];
    }];

}
- (void)handleWithData:(NSArray *)arr{
    
    [self.dataArray removeAllObjects];
    
    for (NSDictionary *subDic in arr) {
        ContactModel *model=[[ContactModel alloc] initWithDic:subDic];
      
        [self.dataArray addObject:model];
    }
    
    _rowArr=[ContactDataHelper getFriendListDataBy:self.dataArray];
    _sectionArr=[ContactDataHelper getFriendListSectionBy:[_rowArr mutableCopy]];
}

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_rowArr[section] count];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //viewforHeader
    id label = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    if (!label) {
        label = [[UILabel alloc] init];
        [label setFont:[UIFont systemFontOfSize:14.5f]];
        [label setTextColor:[UIColor grayColor]];
        [label setBackgroundColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1]];
    }
    [label setText:[NSString stringWithFormat:@"  %@",_sectionArr[section+1]]];
    return label;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _rowArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 39;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
//    "departmentName" : "信息中心",
//    "departmentNo" : "10"
    ContactModel *model = _rowArr[indexPath.section][indexPath.row];
    cell.textLabel.text =model.departmentName;
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
//    NSDictionary *dict =self.dataArray[indexPath.row];
    ContactModel *model =_rowArr[indexPath.section][indexPath.row];
    
    if (self.callBack) {
        self.callBack(model.departmentNo,model.departmentName);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
        return _sectionArr;
  
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index-1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
        return 22.0;
   
}

@end
